\documentclass[11pt,a4paper,oneside]{report}

\usepackage{listings}
\usepackage{color}

\newcommand{\code}[1]{\texttt{#1}}

\lstset{ %
language=JAVA,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=2,                   % the step between two line-numbers. If it's 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                    % adds a frame around the code
tabsize=2,                    % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)}          % if you want to add a comment within your code
}

\begin{document}
\input{./title.tex}
    \section*{Code}
    
    Source code can be accesed at: \code{/home/rogu1637/CompilerPrj/assignment3/} \code{uC-compiler-semantic}
    
	We are using the supplied Java Framework. Based on the supplied \code{Semantic.java}, \code{Env.java}, \code{GlobalEnv.java}, \code{LocalEnv.java}, \code{Type.java} files under the \code{absyn} directory, we rewrote the following classes :
	\subsection*{Semantic.java}
	\lstinputlisting{../absyn/Semantic.java}
	\subsection*{Type.java}
	\lstinputlisting{../absyn/Type.java}
	\subsection*{SemanticException.java}
	We also defined an exception in \code{SemanticException.java} to handle semantic errors.
	\lstinputlisting{../absyn/SemanticException.java}	
	
    \newpage
    
\section*{Report}
The approach solution to the problem was given:
\begin{itemize}
	\item Enforcing the type system.
	\item Checking the use of primitive operations.
	\item Maintaining the environment (symbol table).
	\item Collecting information from declarations.
	\item Detecting and reporting errors.
\end{itemize}

To enforce the type system and to be able to assign a type to any expression, we have created a class for each possible type. This task was not difficult since the type system is quite simple in uC. Basically, we have \code{int} and \code{char} for scalar variables; \code{int} and \code{char} for array variables; and \code{int}, \code{char} and \code{void} for functions. Since a function call is also an expression, and a function can return \code{void}, we included a type called \code{void} that can be assigned to this kind of expressions only. These classes are:

\begin{verbatim}
	public class Type
	class IntType extends Type
	class CharType extends Type
	class VoidType extends Type
	class ArrayType extends Type
	class IntArrayType extends ArrayType
	class CharArrayType extends ArrayType
	class FunctionType extends Type
	class IntFunctionType extends FunctionType
	class CharFunctionType extends FunctionType
	class VoidFunctionType extends FunctionType
\end{verbatim}

The check the use of primitive operators, considering that operations can only be applied to \code{char} and \code{int}, we basically check that the operands are valid using the implemented method \code{static void checkValidOperand(Type exprType)  throws SemanticException}. For the assignment this is a little more complicated since type conversion should be handled, in this case we check for type conversion case by case.

Concerning the envirnoment, we first create a global symbol table as an object of class \code{GlobalEnv}, and we fill it with the top declarations (see the grammar) as we traverse the tree. Each time we encounter the body of a function, we create an local symbol table as an object of class \code{LocalEnv}, and we fill it with the parameters and local declarations.

All declarations are collected as we traverse the tree, in that order. These are stored in the corresponding symbol table. For declaration of functions, note that the \code{Type} object stored in the symbol table contains also information about its parameters (number and types).

To detect semantic errors, we simply raise and exception containing an error message when the firs error is encountered, and then we print the message.

\subsection*{Information associated with identifiers in the environment}
\begin{itemize}
	\item For each scalar identifier we associate its name and type.
	\item For each array identifier we associate its name, type and size.
	\item For each function we associate its name, (return) type, and the information of its parameters. For each parameter we store its name and type. We don't care of the size of an array parameter, so we just store a zero as the size.
\end{itemize}

\subsection*{Implementation of environment (symbol table)}
The global and local environments are implemented as hash tables with the name of the identifier as keys. Each entry of the hash table has the \code{Type} for the corresponding identifier.
A local environment has a pointer to the global environment.

\subsection*{Handling of delimited scopes}
We create an environment for each function body. When we quit the function, we simple destroy the local environment. To check if an identifier is valid, we first look up for the identifier information in the local table (if any), and if it is not there we look up for the identifier information in the global table.

\subsection*{Typing rules}
The implemented typing rules are:
\begin{itemize}
	\item Any index must be convertible to int
	\item If the l-value is an identifier it must have the type int or char.
	\item A function call must have the same number of arguments and the same type of arguments in the call as in the definition
	\item In the body of a function definition each return statement must return a value convertible to the type given in the type definition
	\item Type conversion, only between identical types, int and char, and arrays that store the same data but have different dimensions 
	\item Unary and binary operations can only be applied to char and int
	\item Each identifier must be declared before used, and an identifier can be declared only once in the same environment.
\end{itemize}

    \newpage
\section*{Test Runs}
\subsection*{quiet/semantic/*.c}
	For all test files in directory quiet/semantic/, they do not have the return statements in main functions, which expects a return value of int (as you defined). As a result, it will cause semantical errors. We think this should be wrong. Is this really the 'quiet' files?

\subsection*{incorrect/semantic/se01.c}

	Semantic Error: b has not been defined


\subsection*{incorrect/semantic/se02.c}
	Semantic Error: Function foo has not been defined.


\subsection*{incorrect/semantic/se03.c}
	Semantic Error: Function output has not been defined.


\subsection*{incorrect/semantic/se04.c}
	Semantic Error: Identifier a doubly defined


\subsection*{incorrect/semantic/se05.c}
	Semantic Error: Identifier a doubly defined

\subsection*{incorrect/semantic/se06.c}
	Semantic Error: Identifier a doubly defined


\subsection*{incorrect/semantic/se07.c}
	Semantic Error: The function is void but it returns something


\subsection*{incorrect/semantic/se08.c}
	Semantic Error: The function isn't void but retuns nothing


\subsection*{incorrect/semantic/se09.c}
	Semantic Error: The function returns an invalid type.



\subsection*{incorrect/semantic/se10.c}
	Semantic Error: n is not an array


\subsection*{incorrect/semantic/se11.c}
	Semantic Error: lvalue can't be a function


\subsection*{incorrect/semantic/se12.c}
	Semantic Error: a is not a function.


\subsection*{incorrect/semantic/se13.c}
	Semantic Error: Invalid operand.


\subsection*{incorrect/semantic/se14.c}
	Semantic Error: f is not a function.



\subsection*{incorrect/semantic/se15.c}
	Semantic Error: q takes 3 parameters.


\subsection*{incorrect/semantic/se16.c}
	Semantic Error: d takes 2 parameters.


\subsection*{incorrect/semantic/se17.c}
	Semantic Error: Invalid operand.

\subsection*{incorrect/semantic/se18.c}
	Semantic Error: Can't assign a value to the array variable .


\subsection*{incorrect/semantic/se19.c}
	Semantic Error: Invalid operand.


\subsection*{incorrect/semantic/se20.c}
	Semantic Error: Only can convert between arrays that store the same data but have different known dimensions

\subsection*{incorrect/semantic/se21.c}
	Semantic Error: The function returns an invalid type.


\subsection*{incorrect/semantic/se22.c}
	Semantic Error: Invalid operand.


\subsection*{incorrect/semantic/se23.c}
	Semantic Error: b is not an array


\subsection*{incorrect/semantic/se24.c}
	Semantic Error: Only can convert between arrays that store the same data but have different known dimensions


\subsection*{incorrect/semantic/se25.c}
	Semantic Error: Invalid lvalue.


\subsection*{incorrect/semantic/se26.c}
	Semantic Error: f takes wrong  parameters.


\subsection*{incorrect/semantic/se27.c}
	Semantic Error: The function is void but it returns something


\subsection*{incorrect/semantic/se28.c}
	Semantic Error: The function is void but it returns something


\subsection*{incorrect/semantic/se29.c}
	Semantic Error: Return statement is missing


\subsection*{incorrect/semantic/se30.c}
	Semantic Error: Can't assign an array to a scalar variable .


\subsection*{incorrect/semantic/se31.c}
	Semantic Error: Identifier a doubly defined


\subsection*{incorrect/semantic/se32.c}
	Semantic Error: Invalid operand.


\subsection*{incorrect/semantic/se33.c}
	Semantic Error: q takes 3 parameters.


\subsection*{incorrect/semantic/se34.c}
	Semantic Error: d takes 2 parameters.

\end{document}